package com.netease.nim.uikit.xpopup.popuview

import android.content.Context
import android.view.View
import android.widget.TextView
import com.lxj.xpopup.core.BottomPopupView
import www.wen.com.common.R
import www.wen.com.common.lib.data.entities.InviteesServerEntity
import www.wen.com.common.lib.extension.glide.setImageUrl
import www.wen.com.common.lib.extension.imgUrl
import www.wen.com.common.lib.extension.setOnClickListenerAgency
import www.wen.com.common.lib.helper.ResultUiCallBack
import www.wen.com.common.lib.view.niceimageview.NiceImageView


/**
 * @author WEN
 * @Description: 加入服务器预览
 * @date 2021/4/25 21:35
 */
class BottomJoinServerPopupView(
        context: Context,
        private val dismissAfterConfirm:Boolean = false,
        private val serverEntity: InviteesServerEntity,
        private val resultCallBack: ResultUiCallBack<InviteesServerEntity>? = null,
) : BottomPopupView(context), View.OnClickListener {

    private lateinit var mTvUserName:TextView

    private lateinit var mTvStr:TextView

    private lateinit var mTvServerName:TextView

    private lateinit var mTvServerIntroduction:TextView

    private lateinit var mConfirm:TextView

    private lateinit var mCancel:TextView

    private lateinit var mIvAvatar: NiceImageView

    override fun getImplLayoutId(): Int = R.layout.popupview_server

    override fun onCreate() {
        super.onCreate()
        mTvUserName = findViewById(R.id.tvUserName)
        mTvStr = findViewById(R.id.tvStr)
        mIvAvatar = findViewById(R.id.ivAvatar)
        mTvServerName = findViewById(R.id.tvServerName)
        mTvServerIntroduction = findViewById(R.id.tvServerIntroduction)
        mConfirm = findViewById(R.id.confirm)
        mCancel = findViewById(R.id.cancel)

        "【${serverEntity.nickName}】".also { mTvUserName.text = it }
        mTvStr.text = if(serverEntity.joined) "你已加入该服务器" else "邀请你进入服务器"
        setImageUrl(mIvAvatar,serverEntity.serverLogo.imgUrl())
        "【${serverEntity.serverName}】".also { mTvServerName.text = it }
        mTvServerIntroduction.text = serverEntity.serverIntro

        mConfirm.text = if(serverEntity.joined) "进入服务器" else "加入"
        mCancel.text = if(serverEntity.joined) "取消" else "再考虑下"

        setOnClickListenerAgency(this, mConfirm, mCancel)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.confirm -> {
                resultCallBack?.invoke(v,serverEntity,this)
                if(dismissAfterConfirm){
                    dismiss()
                }
            }

            R.id.cancel -> {
                dismiss()
            }

        }
    }

}